package Buffer2;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.Collator;
import java.util.Comparator;
import java.util.Locale;
import java.util.Scanner;
import java.util.TreeSet;

public class Test4 {//
    public static void main(String[] args) throws IOException {//
        TreeSet<Student> set = new TreeSet<>(new Comparator<Student>() {//创建集合对象用来按照制定规则进行排序
            @Override
            public int compare(Student s1, Student s2) {//重写compare方法
                int result = (s2.getMathscore() + s2.getChinesescore()) - (s1.getMathscore() + s1.getChinesescore());//
                if (result == 0) {//
                    result = s2.getMathscore() - s1.getMathscore();
                    if(result==0){
                        result = s2.getChinesescore() - s1.getChinesescore();
                        if (result == 0) {//
                                Collator instance = Collator.getInstance(Locale.CHINA);//
                                result = instance.compare(s2.getName(), s1.getName());
                            }
                    }
                }
                return result;//返回结果
            }
        });
        for (int i = 0; i < 5; i++) {//遍历5个学生对象
            Scanner sc = new Scanner(System.in);//键盘录入对象
            System.out.println("请输入" + (i + 1) + "学生的姓名");//提示语句
            String name = sc.nextLine();//
            System.out.println("请输入" + (i + 1) + "学生的年龄");
            String age = sc.nextLine();//
            System.out.println("请输入" + (i + 1) + "学生的数学成绩");//
            String math = sc.nextLine();//
            System.out.println("请输入" + (i + 1) + "学生的语文成绩");//
            String chinese = sc.nextLine();//
            Student student = new Student(name, Integer.parseInt(age), Integer.parseInt(math), Integer.parseInt(chinese));//
            set.add(student);//将学生对象添加到集合中
        }
        BufferedWriter bw = new BufferedWriter(new FileWriter("IO\\copy6.txt"));//创建字符缓冲输出流对象
        for (Student student : set) {//遍历集合得到每一个学生对象
            StringBuilder sb = new StringBuilder();//创建字符对象
            sb.append(student.getName()).append("#").append(student.getAge()).append("#").append(student.getMathscore()).append("#").append(student.getChinesescore());
        //拼接学生信息
            bw.write(sb.toString());//通过字符流将学生信息写入到文件中
            bw.newLine();//换行
            bw.flush();//刷新

        }
        bw.close();//关闭资源
    }
}
